home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / 4dtnt.zip / FROMDEC.DOC < prev    next >
Text File  |  1991-11-03  |  3KB  |  112 lines

  1.    Convert from decimal to the caller's specified system base.  This is
  2.    an evolutionary step from BUMP and from TODEC.
  3.  
  4. if "%1" == "" .or. "%2" == "" .or. "%[%2]" == "" goto help
  5. set $foo=%@upper[%1]
  6. set $wstr=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
  7.  
  8.     Validate the input variable as being a valid decimal number.
  9.  
  10. gosub valdec
  11. if %$val gt 9 (gosub cleanup ^ goto helpval)
  12.  
  13.     Validate the target number base and set the parameters.
  14.  
  15. iff      %$foo = B then ^ set $foo=2
  16.  elseiff %$foo = O then ^ set $foo=8
  17.  elseiff %$foo = H then ^ set $foo=16
  18.  elseiff %$foo = D then ^ set $foo=10
  19. endiff
  20. iff %$foo gt 1 .and. %$foo lt 37 then ^ gosub base
  21.  else goto help
  22. endiff
  23.  
  24.     Return value to caller.
  25.  
  26. set %2=%$foo ^gosub cleanup^quit 0
  27.  
  28. :cleanup
  29. unset $foo $wstr $val $base >& nul
  30. return
  31.  
  32. :base
  33. set $base=%$foo
  34. gosub cnvtit
  35. return
  36.  
  37.    The "meat" routine.
  38.  
  39. :cnvtit
  40.  
  41.    Set a divisor equal to the target number base.
  42. set $div=%$foo
  43.  
  44.    Standardizing the case is actually irrelevant in decimal, but this IS
  45.    a carryover from BUMP, etc. where it makes sense.
  46. set $num=%@upper[%[%2]]
  47.  
  48.    This step corresponds to initializing the accumulator in TODEC and isn't
  49.    really functional here.
  50. set $temp=0
  51.  
  52.    Initialize the output string.
  53. set $foo=
  54.  
  55. :cnvtloop
  56.  
  57.    Get the remainder (modulo) to get the value of this (rightmost) digit.
  58. set $temp=%@eval[%$num %% %$base]
  59.  
  60.    Use the remainder as an index into the 36 character string to get the
  61.    correct digit character and prepend it to the left of the output 
  62.    string.
  63. set $foo=%@substr[%$wstr,%$temp,1]%$foo
  64.  
  65.    Sleaze off the remainder prior to the next division.  This could be
  66.    handled more elegantly by @int, but @int was added to 4DOS long after
  67.    this was written.
  68. set $num=%@eval[%$num - %$temp]
  69.  
  70.  
  71.    Divide our way down to the next digit to the left
  72. set $num=%@eval[%$num / %$base]
  73.  
  74.    See if we've exhausted the input value.
  75. if %$num gt 0 goto cnvtloop
  76.  
  77. :cnvtexit
  78. unset $div $num $temp >& nul
  79. return
  80.  
  81. :helpval
  82. echo Invalid character detected.
  83. echo .
  84. goto help
  85.  
  86. :help
  87. echo Usage: %@name[%0]  mode  var
  88. echo.
  89. echo   var =  Any environment variable whose value is the input
  90. echo   mode = any of:
  91. echo       B=binary    [0-1]
  92. echo       D=decimal   [0-9]
  93. echo       H=hex       [0-F]
  94. echo       O=octal     [0-7]
  95. echo       [2-36]      [0-(n-1)]
  96. echo New value returned in var, replacing original value
  97. quit 4
  98.  
  99.   See whether a string is nothing but digits 0 - 9 by testing each
  100.   character of the input string for a valid position in wstr.
  101.  
  102. :valdec
  103. set $i=0
  104. :valdoop
  105. set $val=%@index[%$wstr,%@substr[%[%2],%$i,1]]
  106. if %$val lt 0 .or. %$val gt 9 (set $val=99 ^ goto valdexit)
  107. set $i=%@eval[%$i+1]
  108. if %$i lt %@len[%$foo] goto valdoop
  109. :valdexit
  110. unset $i >& NUL
  111. return
  112.